package cn.briarbear.myblog.handle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;


@ControllerAdvice //只能拦截到controller中的异常
public class ExceptionHandle {


    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    /**
     * 捕获异常 如果发生异常,则直接跳转到/error界面
     */
    @ExceptionHandler(Exception.class)
    public ModelAndView handleException(HttpServletRequest request, Model model, Exception e){
        logger.error("系统错误",e);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("exception",e);
        System.out.println("日志");
//        model.addAttribute("msg","发生错误");
        modelAndView.setViewName("error");
        return modelAndView;
    }

}
